package com.example.cash.craft.dao.persist.repository.impl;

import com.example.cash.craft.dao.persist.mapper.ProductMapper;
import com.example.cash.craft.dao.persist.repository.IProductRepository;
import com.example.cash.craft.pojo.entity.Product;
import com.example.cash.craft.pojo.vo.PageData;
import com.example.cash.craft.pojo.vo.ProductVO;
import com.example.cash.craft.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProductRepositoryImpl implements IProductRepository {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public int insert(Product product) {
        return productMapper.insert(product);
    }

    @Override
    public int deleteById(Long id) {
        return productMapper.deleteById(id);
    }

    @Override
    public int updateById(Product product) {
        return productMapper.updateById(product);
    }

    @Override
    public int countByCategoryId(Long categoryId) {
        return productMapper.countByCategoryId(categoryId);
    }

    @Override
    public ProductVO selectById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public PageData<ProductVO> listAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<ProductVO> list = productMapper.listAll();
        PageInfo<ProductVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public PageData<ProductVO> listByCategoryId(Long categoryId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<ProductVO> list = productMapper.listByCategoryId(categoryId);
        PageInfo<ProductVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

}
